package com.tboss.template.system.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.tboss.global.utils.useful.SecurityUtils;
import com.tboss.template.system.dao.TreeviewSysMenuDao;
import com.tboss.template.system.dto.TreeviewSysMenuDto;
import com.tboss.template.system.service.TreeviewSysMenuService;

public class TreeviewSysMenuJsonDaoService implements TreeviewSysMenuService {
	
	private TreeviewSysMenuDao treeviewSysMenuDao;

	@Override
	public StringBuffer build(HashMap<String, String> opts) {
		return null;
	}

	@Override
	public StringBuffer build(HashMap<String, String> opts, TreeviewSysMenuDao treeviewSysMenuDao) {		
		StringBuffer rtnValue = new StringBuffer();
		
		this.treeviewSysMenuDao = treeviewSysMenuDao;
		
		TreeviewSysMenuDto treeviewDto = new TreeviewSysMenuDto();
		treeviewDto.setFnId(opts.get("rootTreeId"));
		treeviewDto.setFnOwner(SecurityUtils.getCurrentUserName());
		List<TreeviewSysMenuDto> list = this.treeviewSysMenuDao.getLeftMenu(treeviewDto);
		
		try {
			Map<String, List<TreeviewSysMenuDto>> menu = new HashMap<String, List<TreeviewSysMenuDto>>();
			menu.put(opts.get("jsonRootKey"), list);
			
			rtnValue.append(new ObjectMapper().writeValueAsString(menu));
			
		} catch (JsonGenerationException e) { e.printStackTrace();
		} catch (JsonMappingException e) { e.printStackTrace();
		} catch (IOException e) { e.printStackTrace();
		} catch (Exception e) { e.printStackTrace();
		}
		
		return rtnValue;
	}

}
